/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
//package oop.demo.bankingtasklistgui;
package BankingTaskListGUI;
/**
 * This class will manage the interaction between the BankAccount and Task Management systems. It will act as a central hub for operations.
 * @author ashongtical
 * @author Anka
 */
//use HashMap for bank_accounts
//use TaskManager to manage tasks

//import packages
import java.util.ArrayList;
import java.util.HashMap;

public class BankingTaskManager {
    // use HashMap for new accounts, task manager should TaskManager type
    private HashMap<String, BankAccount> bankAccounts = new HashMap<>();
    private TaskManager taskManager = new TaskManager();

    //create new HashMap object for accounts and new object for Task Manager
    public BankingTaskManager() {
        // Initialize the bank accounts and task manager
        // No need to initialize as they are already initialized in the declaration
        // bankAccounts = new HashMap<>();
        // taskManager = new TaskManager();
    }

    // Create (add) a new bank account 
    public void createAccount(String accountNumber, double balance, double annualInterestRate) {
        // check if account number already exists
        if (balance < 0) {
            System.out.println("Initial balance must be positive.");
            throw new IllegalArgumentException("Initial balance must be positive.");
        } else if (!bankAccounts.containsKey(accountNumber)) {
            BankAccount newAccount = new BankAccount(balance, annualInterestRate);
            bankAccounts.put(accountNumber, newAccount);
        } else {
            System.out.println("Account number already exists.");
            throw new IllegalArgumentException("Account number already exists.");
        }
    }

    // Deposit money
    // check if account has an account number, if yes get and add balance to the account number
    // Deposits money into an account and adds a related task.
    public void deposit(String accountNumber, double amount) {
        if (bankAccounts.containsKey(accountNumber)) {
            BankAccount account = bankAccounts.get(accountNumber);
            account.deposit(amount);
            taskManager.addTask("Deposit RMB " + amount + " to Account " + accountNumber);
        } else {
            System.out.println("Account number does not exist.");
            throw new IllegalArgumentException("Account number does not exist.");
        }
    }

    // Withdraw money
    // check if account has an account number, if yes get and subtract the amount from the balance of the account number
    // Withdraws money from an account and adds a related task.
    public void withdraw(String accountNumber, double amount) {
        if (bankAccounts.containsKey(accountNumber)) {
            BankAccount account = bankAccounts.get(accountNumber);
            account.withdraw(amount);
            taskManager.addTask("Withdrawal RMB " + amount + " from Account " + accountNumber);
        } else {
            System.out.println("Account number does not exist.");
            throw new IllegalArgumentException("Account number does not exist.");
        }
    }

    // View account details
    // check if account has an account number, if yes get and print the details
    public void viewAccountDetails(String accountNumber) {
        if (bankAccounts.containsKey(accountNumber)) {
            BankAccount account = bankAccounts.get(accountNumber);
            System.out.println("Account Number: " + accountNumber);
            System.out.println("Balance: " + account.getBalance());
            System.out.println("Annual Interest Rate: " + account.getAnnualInterestRate());
            System.out.println("Deposits Count: " + account.getDepositsCount());
            System.out.println("Withdrawals Count: " + account.getWithdrawalsCount());
        } else {
            System.out.println("Account number does not exist.");
        }
    }

    // Get balance
    // Added this method for BankingService to get balance
    public Double getBalance(String accountNumber) {
        if (bankAccounts.containsKey(accountNumber)) {
            BankAccount account = bankAccounts.get(accountNumber);
            return account.getBalance();
        } else {
            throw new RuntimeException("Account number does not exist.");
        }
    }

    // Monthly processing
    // Performs the monthly process for an account.
    public void monthlyProcess(String accountNumber) {
        if (bankAccounts.containsKey(accountNumber)) {
            BankAccount account = bankAccounts.get(accountNumber);
            account.monthlyProcess();
            taskManager.addTask("Monthly processing for account " + accountNumber);
        } else {
            System.out.println("Account number does not exist.");
            throw new IllegalArgumentException("Account number does not exist.");
        }
    }

    // Manage tasks
    public void viewTasks() {
        taskManager.displayTasks();
    }
    
    // New method to add a task
    public void addTask(String task) {
        taskManager.addTask(task);
    }

    // New method to remove a task
    public void removeTask(String priority, int index) {
        taskManager.removeTask(priority, index);
    }

    // New method to change task priority
    public void changePriority(String priority, int index) {
        taskManager.changePriority(priority, index);
    }

    // New method to promote a task
    public void promoteTask(int index) {
        taskManager.promoteTask(index);
    }

    // New method to get high-priority tasks
    public ArrayList<String> getHighPriorityTasks() {
        return taskManager.getHighPriorityTasks();
    }

    // New method to get low-priority tasks
    public ArrayList<String> getLowPriorityTasks() {
        return taskManager.getLowPriorityTasks();
    }

}
